package com.shelpe.services.sustainer.step;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.sustainer.repository.jpa.InprogressJobRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BlankStarterStep extends AbstractStep {

	@Autowired
	private InprogressJobRepository inprogressJobRepository;
	
	@Override
	protected void doExecute(StepExecution stepExecution) throws Exception {
		if(log.isDebugEnabled()){
			log.debug("starting job {}: {}", stepExecution.getJobExecution().getJobInstance().getJobName(),
					stepExecution.getJobExecutionId());
		}
		String jobName = stepExecution.getJobExecution().getJobInstance().getJobName();
		int count = this.inprogressJobRepository.countByJobName(jobName);
		log.debug("inrpogress jobs: {} - {}", count, 1);
		if(count > 1){
			stepExecution.setExitStatus(ExitStatus.COMPLETED);
			stepExecution.setStatus(BatchStatus.STOPPED);
		}
	}

}
